# coding=utf-8

import pika


class Receiver():
    def __init__(self, host='localhost'):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=host))
        self.channel = self.connection.channel()
        self.queue = self.channel.queue_declare()

    def receive(self, type="#"):
        self.channel.exchange_declare(exchange='log',exchange_type='topic')
        self.channel.queue_bind(exchange='log', queue=self.queue.method.queue, routing_key=type)
        self.channel.basic_consume(consumer_callback=self.callback, queue=self.queue.method.queue)
        try:
            self.channel.start_consuming()
        except:
            self.channel.stop_consuming()
            self.connection.close()
            print 'connection close'

    def callback(self, ch, method, props, body):
        print 'ch:', repr(ch)
        print 'method:', repr(method)
        print 'props:', repr(props)
        print 'body:', repr(body)
        print '\n'


if __name__ == '__main__':
    receiver = Receiver()
    receiver.receive()
    print "the end"
